<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.name" placeholder="角色名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="findList">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="addCompany">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>
    <!--列表-->
    <el-table
      :data="list"
      highlight-current-row
      v-loading="listLoading"
      style="width: 100%;"
      height="70vh"
    >
      <el-table-column prop="name" label="角色名称" width="120" align="center"></el-table-column>
      <el-table-column prop="description" label="描述" align="center"></el-table-column>
      <el-table-column prop="sort" label="序号" align="center"></el-table-column>
      <el-table-column label="操作" width="250" align="center">
        <template slot-scope="scope">
          <el-button size="small" @click="handDelete(scope.row)">删除</el-button>
          <el-button type="danger" size="small" @click="handEdit(scope.row)">编辑</el-button>
          <el-button size="small" @click="configure(scope.row)">权限配置</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-col :span="24" class="toolbar">
      <!-- <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button> -->
      <el-pagination
        layout="prev, pager, next"
        @current-change="handleCurrentChange"
        :page-size="20"
        :total="total"
        style="float:right;"
      ></el-pagination>
    </el-col>
    <!--新增 编辑界面-->
    <el-dialog title="角色" :visible.sync="addFormVisible" :close-on-click-modal="false">
      <addRole @hideTotas="hideTotas" v-if="addFormVisible" :sendFormData="sendFormData"></addRole>
    </el-dialog>
    <!-- 权限配置 -->
    <el-dialog title="权限配置" :visible.sync="powerVisible" :close-on-click-modal="false">
      <el-form :model="form">
        <el-form-item label="分配菜单">
          <el-select v-model="form.menuIds" multiple placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.menuId"
              :label="item.name"
              :value="item.menuId"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="powerVisible = false">取 消</el-button>
        <el-button type="primary" @click="addRoleMenu">确 定</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
import addRole from "./components/addRole";
import {
  companyList,
  getRoleList,
  delRole,
  menuList,
  setAuthority
} from "../../api/baseSetting";

export default {
  data() {
    return {
      filters: {
        name: ""
      },
      //权限配置
      form: {
        menuIds: [],
        roleId: ""
      },
      options: [], //选择路由
      addFormVisible: false,
      powerVisible: false, //权限配置弹出
      list: [],
      total: 0,
      page: 1,
      listLoading: false,
      sendFormData: {
        //点中的公司参数
      }
    };
  },
  components: {
    addRole
  },
  methods: {
    //点击删除
    handDelete(e) {
      let roleId = e.roleId;
      this.$confirm("确认删除该角色", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          delRole({ roleId }).then(res => {
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.filters.name = "";
            this.page = 1;
            this.getList();
          });
        })
        .catch(() => {
          this.$message({
            type: "fail",
            message: "删除失败!"
          });
          this.$store.commit("setCompanyList", []);
          this.page = 1;
          this.filters.companyName = "";
          this.getList();
        });
    },
    //点击编辑
    handEdit(detail) {
      this.sendFormData = detail;
      this.addFormVisible = true;
    },
    //点击权限分配
    configure(e) {
      this.powerVisible = true;
      this.form.roleId = e.roleId;
    },
    //分页
    handleCurrentChange(val) {
      this.page = val;
      this.getList();
    },
    //隐藏新增表单
    hideTotas() {
      this.addFormVisible = false;
      this.filters.companyName = "";
      this.page = 1;
      this.getList();
    },
    //点击新增
    addCompany() {
      this.addFormVisible = true;
    },
    //点击查询
    findList() {
      this.page = 1;
      this.getList();
    },
    //获取角色列表
    getList() {
      let para = {
        name: this.filters.name,
        page: {
          page: this.page,
          pageSize: 10
        }
      };
      this.listLoading = true;
      getRoleList(para)
        .then(res => {
          this.listLoading = false;
          this.list = res.records;
          this.total = res.total;
        })
        .catch(err => {
          this.listLoading = false;
        });
    },
    //获取菜单列表  用于分配权限
    getMenu() {
      let para = {
        page: {
          page: this.page,
          pageSize: 10000
        }
      };
      menuList(para).then(res => {
        this.options = res.records;
      });
    },
    //点击确认 角色分配菜单
    addRoleMenu() {
      setAuthority(this.form).then(res => {
        this.$message({
          type: "success",
          message: "成功"
        });
        this.powerVisible = false;
        this.page = 1;
        this.form = {
          menuIds: [],
          roleId: ""
        };
        this.getList();
      });
    }
  },
  mounted() {
    this.getList();
    this.getMenu();
  }
};
</script>
<style lang="less" scoped>
</style>
